/*   Copyright (C) 2011 Wolfgang Hankeln, Norma J. Wendel
 *
 *   This file is part of CDinFusion.
 *
 *   CDinFusion is free software: you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public 
 *   License in version 3 (LGPL3) as published by the Free Software
 *   Foundation version 3 of the License.
 *
 *   CDinFusion is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the Lesser General Public
 *   License along with CDinFusion; see the file COPYING. If not,
 *   write to the Free Software Foundation, 59 Temple Place - Suite 330,
 *   Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
 *   
 *   If you have any questions, send an email to: megx@mpi-bremen.de.
 */
package org.cdinfusion.data.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

public final class CDinFusionExceptionResolver implements
		HandlerExceptionResolver {

	public ModelAndView resolveException(final HttpServletRequest request,
			final HttpServletResponse response, final Object handler,
			final Exception e) {

		final ModelAndView result;
		// the NoSuchFlowExecutionException is thrown every time a user goes
		// back to the upload form again --> normal error, do not log
		if ((e instanceof org.springframework.webflow.execution.repository.NoSuchFlowExecutionException)) {
			final HttpSession session = request.getSession();
			result = new ModelAndView("redirect:upload.htm?_flowId=fileupload&sessionID=" + session.getId());
			session.setAttribute("origin", "uploadfasta");
		} else {
			System.out.println(e);
			e.printStackTrace();
			response.setStatus(500);
			result = new ModelAndView("UnexpectedErrorPage");
		}

		return result;
	}

}
